using Godot;
using System;

public partial class NInventoryPanel : Panel
{
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}

	void _on_gui_input(InputEvent @event)
	{
		if (@event is InputEventMouseButton mb)
		{
			if (mb.Pressed && mb.ButtonIndex == MouseButton.Left)
			{
				NItem curSelItem = CHelp.getNodeInGroup<NItem>(this, CHelp.CURRENT_SEL_ITEM);
				NItem item = Item;

				if (curSelItem == null)
				{
					if (item != null)
					{
						item.ZIndex = 1;
						CHelp.addToGroup(item, CHelp.CURRENT_SEL_ITEM);
					}
				}
				else
				{
					if (item == null)
					{
						curSelItem.Reparent(this);
						curSelItem.Position = Vector2.Zero;
						curSelItem.ZIndex = 0;
					}
					else
					{
						NInventoryPanel curSelItemPanel = curSelItem.GetParent() as NInventoryPanel;
						curSelItem.Reparent(this);
						curSelItem.Position = Vector2.Zero;
						curSelItem.ZIndex = 0;
						item.Reparent(curSelItemPanel);
						item.Position = Vector2.Zero;
						item.ZIndex = 0;
					}

					curSelItem.RemoveFromGroup(CHelp.CURRENT_SEL_ITEM);
				}
			}
		}
	}

	public NItem Item
	{
		get
		{
			if (GetChildCount() > 0)
			{
				return GetChild(0) as NItem;
			}
			else
			{
				return null;
			}
		}
	}

}
